package com.woniuxy.user.service.impl;

import com.woniuxy.user.client.FlowableFeignClient;
import com.woniuxy.user.service.ApprovalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 审批服务实现类
 * 使用OpenFeign进行远程调用
 */
@Slf4j
@Service
public class ApprovalServiceImpl implements ApprovalService {

    @Autowired
    private FlowableFeignClient flowableFeignClient;

    @Override
    public List<Map<String, Object>> getPendingTasks(String currentUser) {
        try {
            Map<String, Object> response = flowableFeignClient.getPendingTasks(currentUser);
            if (response != null && (Boolean) response.get("success")) {
                @SuppressWarnings("unchecked")
                List<Map<String, Object>> data = (List<Map<String, Object>>) response.get("data");
                return data;
            } else {
                log.warn("获取待办任务失败，响应：{}", response);
                return new ArrayList<>();
            }
        } catch (Exception e) {
            log.error("获取待办任务失败", e);
            throw new RuntimeException("获取待办任务失败: " + e.getMessage());
        }
    }

    @Override
    public List<Map<String, Object>> getCompletedTasks(String currentUser) {
        try {
            Map<String, Object> response = flowableFeignClient.getCompletedTasks(currentUser);
            if (response != null && (Boolean) response.get("success")) {
                @SuppressWarnings("unchecked")
                List<Map<String, Object>> data = (List<Map<String, Object>>) response.get("data");
                return data;
            } else {
                log.warn("获取已办任务失败，响应：{}", response);
                return new ArrayList<>();
            }
        } catch (Exception e) {
            log.error("获取已办任务失败", e);
            throw new RuntimeException("获取已办任务失败: " + e.getMessage());
        }
    }

    @Override
    public void approveTask(String taskId, String comment, String currentUser) {
        try {
            Map<String, Object> response = flowableFeignClient.approveTask(taskId);
            if (response != null && (Boolean) response.get("success")) {
                log.info("审批通过成功，任务ID：{}，审批人：{}", taskId, currentUser);
            } else {
                log.warn("审批通过失败，响应：{}", response);
                throw new RuntimeException("审批通过失败");
            }
        } catch (Exception e) {
            log.error("审批通过失败", e);
            throw new RuntimeException("审批通过失败: " + e.getMessage());
        }
    }

    @Override
    public void rejectTask(String taskId, String reason, String currentUser) {
        try {
            Map<String, Object> response = flowableFeignClient.rejectTask(taskId, reason);
            if (response != null && (Boolean) response.get("success")) {
                log.info("审批拒绝成功，任务ID：{}，审批人：{}，拒绝原因：{}", taskId, currentUser, reason);
            } else {
                log.warn("审批拒绝失败，响应：{}", response);
                throw new RuntimeException("审批拒绝失败");
            }
        } catch (Exception e) {
            log.error("审批拒绝失败", e);
            throw new RuntimeException("审批拒绝失败: " + e.getMessage());
        }
    }

    @Override
    public Map<String, Object> getApprovalHistory(Map<String, Object> params) {
        try {
            Map<String, Object> response = flowableFeignClient.getApprovalHistory(params);
            if (response != null && (Boolean) response.get("success")) {
                @SuppressWarnings("unchecked")
                Map<String, Object> data = (Map<String, Object>) response.get("data");
                return data;
            } else {
                log.warn("获取审批历史失败，响应：{}", response);
                return new HashMap<>();
            }
        } catch (Exception e) {
            log.error("获取审批历史失败", e);
            throw new RuntimeException("获取审批历史失败: " + e.getMessage());
        }
    }

    @Override
    public Map<String, Object> getApprovalStatistics(String currentUser) {
        try {
            Map<String, Object> response = flowableFeignClient.getApprovalStatistics(currentUser);
            if (response != null && (Boolean) response.get("success")) {
                @SuppressWarnings("unchecked")
                Map<String, Object> data = (Map<String, Object>) response.get("data");
                return data;
            } else {
                log.warn("获取审批统计失败，响应：{}", response);
                return new HashMap<>();
            }
        } catch (Exception e) {
            log.error("获取审批统计失败", e);
            throw new RuntimeException("获取审批统计失败: " + e.getMessage());
        }
    }
}
